package com.zhoug.zmui.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListPopupWindow;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zhoug.common.adapters.BaseListViewAdapter;
import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

/**
 * 自动提示的输入框
 *
 * @Author 35574
 * @Date 2020/8/11
 * @Description
 */
public abstract class ZMUIAutoComplete<T> extends ListPopupWindow {
    private static final String TAG = ">>>ZMUIAutoComplete";
    private Context context;
    private List<T> data = new ArrayList<>();//数据集
    private BaseListViewAdapter<T> adapter;
    private boolean init = false;//是否完成初始化
    private boolean showDivider = true;//是否显示分割线
    private int dividerHeight = 1;//分割线高度
    private int dividerColor = Color.parseColor("#dddddd");//分割线颜色
    private Drawable background;//背景

    public ZMUIAutoComplete(@NonNull Context context) {
        super(context);
        this.context = context;
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setHeight(AppUtils.getScreenSize().y / 3);//默认高度
        background = ContextCompat.getDrawable(getContext(), R.drawable.zmui_card);
    }

    /**
     * 初始化
     */
    private void init() {
        if (!init) {
            init = true;
            if (adapter == null) {
                adapter = new DefAdapter();
            }
            adapter.setData(data);
            setModal(true);
            setAdapter(adapter);
            setFocusable(false);
            if(background!=null){
                setBackgroundDrawable(background);
            }
        }
    }

    public void setData(List<T> data) {
        this.data.clear();
        if (data != null && data.size() > 0) {
            this.data.addAll(data);
        }
    }

    public List<T> getData() {
        return data;
    }

    @Override
    @Deprecated
    public void setAdapter(@Nullable ListAdapter adapter) {
        super.setAdapter(adapter);
    }

    public void setAdapter1(BaseListViewAdapter<T> adapter) {
        this.adapter = adapter;
    }

    public BaseListViewAdapter<T> getAdapter() {
        return adapter;
    }

    public Context getContext() {
        return context;
    }

    public void notifyDataSetChanged() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void setShowDivider(boolean showDivider) {
        this.showDivider = showDivider;
    }

    public void setDividerColor(int dividerColor) {
        this.dividerColor = dividerColor;
    }

    public void setDividerHeight(int dividerHeight) {
        this.dividerHeight = dividerHeight;
    }

    @Override
    public void setBackgroundDrawable(@Nullable Drawable d) {
        this.background=d;
        super.setBackgroundDrawable(d);
    }

    @Override
    public void show() {
        init();
        super.show();
        ListView listView = getListView();
        if (listView != null && showDivider) {
//            listView.setPadding(20,0,20,0);
            listView.setDivider(new ColorDrawable(dividerColor));
            listView.setDividerHeight(dividerHeight);
        }

    }

    public void setFocusable(boolean focusable) {
        try {
            Field mPopupField = ListPopupWindow.class.getDeclaredField("mPopup");
            mPopupField.setAccessible(true);
            PopupWindow mPopup = (PopupWindow) mPopupField.get(this);
            mPopup.setFocusable(focusable);
            mPopupField.setAccessible(false);
            LogUtils.d(TAG, "setFocusable:" + focusable);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private class DefAdapter extends BaseListViewAdapter<T> {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.zmui_auto_complete_item, parent, false);
            }
            TextView textView = convertView.findViewById(R.id.item);
            T item = getItem(position);
            textView.setText(getText(item));
            return convertView;
        }
    }


    public abstract String getText(T itemData);


}

